import indexPage
import requests
import json
import tkinter
import tkinter.messagebox

from tkinter import ttk
from tkinter import *
from infoPage import *
from config import *


class ListPage(object):

    def __init__(self, master=None):
        # 定义内部变量
        self.root = master
        self.pageNo = 1
        self.pageSize = 35
        self.createPage()

    def createPage(self):
        self.frame = Frame(self.root, height=800)
        self.frame.pack()
        lineWidth = 200
        # 创建表格
        self.tree = ttk.Treeview(self.frame,
                            height=35,
                            show="headings",
                            columns=("game_name",
                                     "game_type",
                                     "game_score",
                                     "game_launch_date",
                                     "game_brief_introduction"))
        # 定义列
        self.tree.column("game_name", width=lineWidth, anchor='center')
        # 列名与显示列标题绑定
        self.tree.heading("game_name", text="游戏名字")
        self.tree.column("game_type", width=100, anchor='center')
        self.tree.heading("game_type", text="游戏类型")
        self.tree.column("game_score", width=100, anchor='center')
        self.tree.heading("game_score", text="游戏评分")
        self.tree.column("game_launch_date", width=100, anchor='center')
        self.tree.heading("game_launch_date", text="更新日期")
        self.tree.column("game_brief_introduction", width=600, anchor='center')
        self.tree.heading("game_brief_introduction", text="游戏简介")

        # 每一行数据的点击事件
        def lineClick(a):
            item = self.tree.selection()
            if item:
                InfoPage.data = item[0]
                self.frame.destroy()
                InfoPage(self.root)
        # 绑定点击事件
        self.tree.bind('<ButtonRelease-1>', lineClick)

        # 填充列表数据 start
        list = self.get_list()
        # 填充列表数据 end

        if list is None:
            self.update_list()
            a = tkinter.messagebox.showinfo('提示', '暂无数据,已开启爬取数据任务,请稍后重启')
            if a == 'ok':
                self.root.destroy()
        elif list == 0:
            self.root.destroy()
        else:
            total_number_chart = Button(self.frame, text='查看评分占比统计图', command=self.toIndexPage)
            total_number_chart.grid(row=2, column=0, sticky=W)

            previous = Button(self.frame, text='上一页', command=self.previous_page)
            previous.grid(row=2, column=0, sticky=E)
            next = Button(self.frame, text='下一页', command=self.next_page)
            next.grid(row=2, column=1, sticky=W)

    # 请求列表数据
    def get_list(self):
        url = '/getlist'
        param = {'pageNo': self.pageNo, 'pageSize': self.pageSize}
        headers = {'user-agent': Config.get_ua(Config)}
        try:
            ret = requests.get(Config.beas_url + url, params=param, headers=headers)
            retJson = json.loads(ret.text)
            list = retJson.get('data')
            if (list is None) or (len(list) < 1):
                return None
            else:
                x = self.tree.get_children()
                for item in x:
                    self.tree.delete(item)

                # 放入数据
                for d in list:
                    self.tree.insert('', int(d['game_id']), iid=int(d['game_id']), values=(
                        d['game_name'], d['game_tab'], d['game_score'], d['game_date'], d['game_brief_introduction']))
                    self.tree.grid()
                return list
        except Exception as e:
            print(e)
            a = tkinter.messagebox.showerror('错误', '网络异常')
            return 0

    # 上一页
    def previous_page(self):
        if self.pageNo > 1:
            self.pageNo = self.pageNo-1
        self.get_list()

    # 下一页
    def next_page(self):
        self.pageNo = self.pageNo+1
        list = self.get_list()
        if list is None:
            self.pageNo = self.pageNo-1

    def toIndexPage(self):
        self.frame.destroy()
        indexPage.IndexPage(self.root)

    # 启动爬取数据任务
    def update_list(self):
        try:
            url = '/updateList'
            headers = {'user-agent': Config.get_ua(Config)}
            ret = requests.get(Config.beas_url + url, headers=headers)
            retJson = json.loads(ret.text)
            if retJson.get('code') == 200:
                tkinter.messagebox.showinfo('提示', '爬取数据任务已开启')
            else:
                print(500)
                tkinter.messagebox.showerror('错误', '网络异常')
        except Exception as e:
            print(e)
            a = tkinter.messagebox.showerror('错误', '网络异常')
